package mydatisdb.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
@Component

@PropertySource("classpath:config.properties")
public class DBUtil implements IDBUtil{
    private String ulr;
    private String username;
    private String password;

    public String getUlr() {
        return ulr;
    }
@Value("${spring.datasource.url}")
    public void setUlr(String ulr) {
        this.ulr = ulr;
    }

    public String getUsername() {
        return username;
    }
@Value("${spring.datasource.username}")
    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }
@Value("${spring.datasource.password}")
    public void setPassword(String password) {
        this.password = password;
    }
    @Value("${spring.datasource.classname}")
    public void setClassname(String classname) {
        try {
            Class.forName(classname);
        }catch (ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    public Connection getConnection() throws SQLException {
        return DriverManager.getConnection(ulr,username,password);
    }
}
